package com.beiding.service;

import com.beiding.dao.ResourceOperations;
import com.beiding.dao.GenerationDao;
import com.beiding.dao.ResourceDao;
import com.beiding.pojo.Group;
import com.beiding.pojo.StaticResource;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.io.File;
import java.security.Principal;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Service
public class StaticResourceServiceImpl implements StaticResourceService{

    private GenerationDao generationDao;
    private ResourceDao resourceDao;

    public StaticResourceServiceImpl(GenerationDao generationDao, ResourceDao resourceDao) {
        this.generationDao = generationDao;
        this.resourceDao = resourceDao;
    }

    @Override
    public List<String> saveResourceGroup(Group resourceGroup) {

        //首先在数据库中保存
        resourceDao.saveAll(resourceGroup.getMembers());
        resourceGroup.persistent();
        List<String> memberIds = resourceGroup.getMemberIds();
        generationDao.addTemp(memberIds);
        return memberIds;
    }

    @Override
    public List<String> remove(Principal principal, String[] ids) {

        List<String> r = new ArrayList<>();

        //把资源反向认领丢入当前代中
        for (String s:ids)
        {
            try {
                checkHolder(s, principal.getName());
                resourceDao.claim(s, null, false);
                generationDao.addTemp(s);
                r.add(s);
            } catch (Exception ignore) {
            }
        }
        return r;
    }

    //批量认领.返回认领成功的资源ID
    @Override
    public List<String> claim(Principal principal, List<String> resourceId) {
        List<String> success = new ArrayList<>();
        for (String s : resourceId) {
            generationDao.removeTemp(s);
            resourceDao.claim(s, principal.getName(), true);
            success.add(s);
        }
        return success;
    }


    //迭代任务.每隔五分钟执行一次迭代, 逻辑上临时文件最大保留时长为10分钟,即当前代和父代文件有效
    @Scheduled(fixedRate = 5 * 60 * 1000)
    private void iterativeTask() {

        //删除当前代的爷爷代
        Set<String> idSet = generationDao.alternate(2);
        if (idSet != null) {
            idSet.forEach(s -> {
                File f = get(s,"fileName").absolutePathFile();

                boolean flag = true;

                if (f.exists())
                    flag = f.delete();

                if (flag)
                    for (int i = 1; i < 6; i++) {
                        File file = new File(f.getAbsolutePath() + "-" + i);
                        if (file.exists()) {
                            if (!file.delete()) {
                                flag = false;
                                break;
                            }
                        }
                    }


                if (flag) {//如果所有相关的文件全部清除则移除.否则重新添加进临时文件
                    resourceDao.deleteById(s);
                } else {
                    resourceDao.claim(s, null, false);
                    generationDao.addTemp(s);
                }


            });

        }
    }

    @Override
    public ResourceOperations<StaticResource> resourceOperations() {
        return resourceDao;
    }

}
